package com.example.audiostreaming.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.EncryptUtils;
import com.example.audiostreaming.Bean.UserBean;
import com.example.audiostreaming.R;
import com.example.audiostreaming.base.BaseActivity;
import com.example.audiostreaming.utils.UtilUser;
import com.example.audiostreaming.views.InputView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends BaseActivity {
    private static final String TAG = "RegisterActivity";
    private InputView mPhone;
    private EditText mName;
    private InputView mPassword;
    private InputView mPasswordConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        initNavBar(true,"注册");
        mPhone = findViewById(R.id.input_phone);
        mName = findViewById(R.id.name);
        mPassword = findViewById(R.id.input_password);
        mPasswordConfirm = findViewById(R.id.input_password_confirm);
    }

    public void onRegisterClick(View view) throws InterruptedException {
        String phone = mPhone.getInputStr();
        String password = mPassword.getInputStr();
        String passwordConfirm = this.mPasswordConfirm.getInputStr();
        String name = mName.getText().toString();

        Boolean isRegisterUser = UtilUser.registerUser(this, phone, name, password, passwordConfirm);

        if (!isRegisterUser) {
            //注册验证未通过
            return;
        }

        onBackPressed();//后退页面

        registerUser(phone,password,name);
    }


    /**
     * 注册用户
     * @param phone
     * @param password
     * @param name
     */
    private void registerUser(String phone, String password, String name) {
        String mUrl = "http://192.168.0.107:8080/AndioStreamingService_war_exploded/";
        OkHttpClient okHttpClient = new OkHttpClient();

        UserBean userBean = UserBean.getUserBean();
        userBean.setTelephone(phone);
//        userBean.setPassword(EncryptUtils.encryptMD5ToString(password));
        userBean.setPassword(password);
        userBean.setName(name);

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        String s = gson.toJson(userBean);

        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=GBK"), s);
        Request.Builder builder = new Request.Builder();
        Request request = builder.url(mUrl + "register").post(requestBody).build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure ==> "+e.getMessage());
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Log.d(TAG, "onResponse: "+string);
            }
        });

    }
}
